package com.example.academicsystem.server.service.impl;

import com.example.academicsystem.constant.MessageConstant;
import com.example.academicsystem.exception.DeleteException;
import com.example.academicsystem.exception.InsertException;
import com.example.academicsystem.exception.UpdateException;
import com.example.academicsystem.pojo.dto.*;
import com.example.academicsystem.pojo.vo.AdminCourseVo;
import com.example.academicsystem.pojo.vo.GradeVo;
import com.example.academicsystem.pojo.vo.StudentVo;
import com.example.academicsystem.pojo.vo.TeacherVo;
import com.example.academicsystem.result.PageResult;
import com.example.academicsystem.server.dao.AdminMapper;
import com.example.academicsystem.server.service.AdminService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    /**
     * 分页查询学生信息
     * @param studentDto
     * @return
     * */
    public PageResult studentPageQuery(StudentDto studentDto) {
        PageHelper.startPage(studentDto.getPage(),studentDto.getPageSize());
        Page<StudentVo> page = adminMapper.studentPageQuery(studentDto);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 删除学生信息及课程—学生信息
     * @param  studentId
     * */
    public void studentDelteById(String studentId) {
        if (studentId==null)
            throw new DeleteException(MessageConstant.DELETE_FAILED+MessageConstant.PARAM_NULL);
        adminMapper.studentDelteById(studentId);
        adminMapper.courseStudentDeleteById(studentId);
    }

    /**
     * delete course_student by student_id
     * */
    public void courseStudentDeleteById(String studentId) {
        if (studentId==null)
            throw new DeleteException(MessageConstant.DELETE_FAILED+MessageConstant.PARAM_NULL);
        adminMapper.courseStudentDeleteById(studentId);
    }

    /**
     * 新增student
     * */
    public void studentInsert(StudentInsertDto studentInsertDto) {
        boolean result= adminMapper.studentInsert(studentInsertDto);
        if (result==false) throw new InsertException(MessageConstant.INSERT_FAILED);
    }

    /**
     * 修改 student by id
     * */
    public void studentUpdateById(StudentUpdateDto studentUpdateDto) {
        if (studentUpdateDto.getStudentId()==null){
            throw new UpdateException(MessageConstant.UPDATE_FAILED+MessageConstant.PARAM_NULL);
        }
        boolean result = adminMapper.studentUpdateById(studentUpdateDto);
        if (result==false) throw new UpdateException(MessageConstant.UPDATE_FAILED);

    }

    /**
     * @param teacherDto
     * @return
     * */
    public PageResult teacherPageQuery(TeacherDto teacherDto) {
        /*if (teacherDto.getTeacherId()==null)
            throw new QueryFailedException(MessageConstant.PARAM_NULL);*/
        PageHelper.startPage(teacherDto.getPage(),teacherDto.getPageSize());
        Page<TeacherVo> page = adminMapper.teacherPageQuery(teacherDto);
        return new PageResult(page.getTotal(),page.getResult());

    }

    /**
     * 新增教师
     * @param teacherInsertDto
     *
     * */
    public void teacherInsert(TeacherInsertDto teacherInsertDto) {
        if (teacherInsertDto.getTeacherId()==null)
            throw new InsertException(MessageConstant.PARAM_NULL);
        boolean result= adminMapper.teacherInsert(teacherInsertDto);
        if (!result) throw new InsertException(MessageConstant.INSERT_FAILED);
    }

    /**
     * 删除教师 by id
     * */
    public void teacherDeleteById(String teacherId) {
        if (teacherId==null || teacherId == ""){
            throw new DeleteException(MessageConstant.DELETE_FAILED);
        }
        adminMapper.teacherDeleteById(teacherId);
        adminMapper.courseTeacherDeleteById(teacherId);
    }

    /**
     * 修改教师 by id
     * */
    public void teacherUpdateById(TeacherUpdateDto teacherUpdateDto) {
        if (teacherUpdateDto.getTeacherId()==null || teacherUpdateDto.getTeacherId()=="")
            throw new UpdateException(MessageConstant.PARAM_NULL);
        adminMapper.teacherUpdateById(teacherUpdateDto);
    }

    /**
     * 删除course-teacher by id
     * */
    public void courseTeacherDeleteById(String teacherId) {
        if (teacherId==null || teacherId=="")
            throw new DeleteException(MessageConstant.PARAM_NULL);
        adminMapper.courseTeacherDeleteById(teacherId);
    }

    /**
     * 分页查询grade
     * */
    public PageResult gradePageQuery(GradeDto gradeDto) {
        PageHelper.startPage(gradeDto.getPage(),gradeDto.getPageSize());
        Page<GradeVo> page  = adminMapper.gradePageQuery(gradeDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增成绩
     * */
    public void gradeInsert(GradeInsertDto gradeInsertDto) {
        if (gradeInsertDto.getScore()==-1)
            throw new InsertException(MessageConstant.PARAM_NULL);
        adminMapper.gradeInsert(gradeInsertDto);
    }

    /**
     * 删除成绩
     * */
    public void gradeDelete(GradeDeleteDto gradeDeleteDto) {
        if (gradeDeleteDto.getStudentId()==""||gradeDeleteDto.getStudentId()==null||gradeDeleteDto.getCourseId()==null||gradeDeleteDto.getCourseId()=="")
            throw new DeleteException(MessageConstant.PARAM_NULL);
        adminMapper.gradeDelete(gradeDeleteDto);
    }
    /**
     * 修改成绩
     * */
    @Override
    public void gradeUpdate(GradeUpdateDto gradeUpdateDto) {
        if (gradeUpdateDto.getStudentId()==""||gradeUpdateDto.getStudentId()==null||gradeUpdateDto.getCourseId()==null||gradeUpdateDto.getCourseId()==""||gradeUpdateDto.getScore()==-1)
            throw new DeleteException(MessageConstant.PARAM_NULL);
        adminMapper.gradeUpdate(gradeUpdateDto);
    }

    /**
     * 新增课程
     * */
    public void courseInsert(CourseInsertDto courseInsertDto) {
        adminMapper.courseInsert(courseInsertDto);
    }

    /**
     * 删除课程
     * */
    @Transactional
    public void courseDelete(CourseDeleteDto courseDeleteDto) {
        if (courseDeleteDto.getCourseId()==null||courseDeleteDto.getCourseId()=="")
            throw new DeleteException(MessageConstant.PARAM_NULL);
        boolean b = courseIndexDelete(courseDeleteDto);
        if (b==true)
            adminMapper.courseDelete(courseDeleteDto);
    }

    /**
     * 分页查询course
     * */
    public PageResult coursePageQuery(AdminCourseDto adminCourseDto) {
        PageHelper.startPage(adminCourseDto.getPage(),adminCourseDto.getPageSize());
        Page<AdminCourseVo> page = adminMapper.coursePageQuery(adminCourseDto);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改course
     * */
    public void courseUpdate(CourseUpdateDto courseUpdateDto) {
        if (courseUpdateDto.getCourseId()==null||courseUpdateDto.getCourseId()=="")
            throw new UpdateException(MessageConstant.PARAM_NULL);
        adminMapper.courseUpdate(courseUpdateDto);
    }

    /**
     * 增加class
     * */
    public void classInsert(Integer id) {
        if (id==null)
            throw new InsertException(MessageConstant.PARAM_NULL);
        adminMapper.classInsert(id);
    }

    private boolean courseIndexDelete(CourseDeleteDto courseDeleteDto){
        adminMapper.courseStudentDeleteByCourseId(courseDeleteDto);
        adminMapper.courseTeacherDeleteByCourseId(courseDeleteDto);
        return true;
    }
}
